
#include <stdio.h>
#include <unistd.h>
#include <ijkplayer/linux/ijkplayer_linux.h>
#include <ijksdl/ijksdl.h>

#ifndef MPTRACE
#define MPTRACE ALOGD
#endif

static int is_quit = 0;

static int loop_msg(void *mp) {
    IjkMediaPlayer *p = mp;

    while (!is_quit) {

        AVMessage msg;

        int retval = ijkmp_get_msg(mp, &msg, 1);
        if (retval < 0)
            break;

        // block-get should never return 0
        assert(retval > 0);

        switch (msg.what) {
        case FFP_MSG_FLUSH:
            MPTRACE("FFP_MSG_FLUSH:");
            //post_event(env, weak_thiz, MEDIA_NOP, 0, 0);
            break;
        case FFP_MSG_ERROR:
            MPTRACE("FFP_MSG_ERROR: %d", msg.arg1);
            //post_event(env, weak_thiz, MEDIA_ERROR, MEDIA_ERROR_IJK_PLAYER, msg.arg1);
            is_quit = true;
            break;
        case FFP_MSG_PREPARED:
            MPTRACE("FFP_MSG_PREPARED:");
            ijkmp_start(p);
            //post_event(env, weak_thiz, MEDIA_PREPARED, 0, 0);
            break;
        case FFP_MSG_COMPLETED:
            MPTRACE("FFP_MSG_COMPLETED:");
            //post_event(env, weak_thiz, MEDIA_PLAYBACK_COMPLETE, 0, 0);
            is_quit = true;
            break;
        case FFP_MSG_VIDEO_SIZE_CHANGED:
            MPTRACE("FFP_MSG_VIDEO_SIZE_CHANGED: %d, %d", msg.arg1, msg.arg2);
            //post_event(env, weak_thiz, MEDIA_SET_VIDEO_SIZE, msg.arg1, msg.arg2);
            break;
        case FFP_MSG_SAR_CHANGED:
            MPTRACE("FFP_MSG_SAR_CHANGED: %d, %d", msg.arg1, msg.arg2);
            //post_event(env, weak_thiz, MEDIA_SET_VIDEO_SAR, msg.arg1, msg.arg2);
            break;
        case FFP_MSG_BUFFERING_START:
            MPTRACE("FFP_MSG_BUFFERING_START:");
            //post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
            break;
        case FFP_MSG_BUFFERING_END:
            MPTRACE("FFP_MSG_BUFFERING_END:");
            //post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
            break;
        case FFP_MSG_BUFFERING_UPDATE:
            // MPTRACE("FFP_MSG_BUFFERING_UPDATE: %d, %d", msg.arg1, msg.arg2);
            //post_event(env, weak_thiz, MEDIA_BUFFERING_UPDATE, msg.arg1, msg.arg2);
            break;
        case FFP_MSG_BUFFERING_BYTES_UPDATE:
            break;
        case FFP_MSG_BUFFERING_TIME_UPDATE:
            break;
        case FFP_MSG_SEEK_COMPLETE:
            MPTRACE("FFP_MSG_SEEK_COMPLETE:");
            //post_event(env, weak_thiz, MEDIA_SEEK_COMPLETE, 0, 0);
            break;
        case FFP_MSG_PLAYBACK_STATE_CHANGED:
            break;
        default:
            ALOGE("unknown FFP_MSG_xxx(%d)", msg.what);
            break;
        }
    }

    is_quit = 1;

    return 0;
}

static int display(SDL_Vout *vout, SDL_VoutOverlay *overlay) {
    //uint32_t color = *((uint32_t *)overlay->pixels[0]);
    //ALOGD("(%dx%d) line=%d color=0x%08X", overlay->w, overlay->h, overlay->pitches[0], color);
    return 0;
}

int main(int argc, const char **argv) {
    const char *pathfile = "/tmp/test.mp4";
    IjkMediaPlayer *p;

    if (argc > 1) pathfile = argv[1];

    ijkmp_global_init();

    p = ijkmp_linux_create(loop_msg);
    if (!p) {
        ALOGE("player create fail!!!");
        goto fail;
    }

    ijkmp_linux_set_display(p, display);

    ijkmp_set_data_source(p, pathfile);
    ijkmp_prepare_async(p);

    while (!is_quit) {
        usleep(100000);
    }

fail:
    ijkmp_dec_ref_p(&p);
    ijkmp_global_uninit();

    return 0;
}
